// Define the `phonecatApp` module
var App = angular.module('studentInfoApp', ['ngRoute'
,'720kb.datepicker'
,'nya.bootstrap.select']);

App.config(['$httpProvider', function ( $httpProvider ) {  
  $httpProvider.defaults.transformRequest = function ( data ) {  
      var str = '';  
      for( var i in data ) {  
          str += i + '=' + data[i] + '&';  
      }  
      return str.substring(0,str.length-1);  
  }  
}]); 

//路由配置

App.config(["$routeProvider",function($routeProvider){  
  $routeProvider.when('/addStudent/:id',{  
      templateUrl:'/view/editstuinfo.jsp',  
      controller:'EditStuInfoCtrl'  
  }).when('/viewStudent',{  
      templateUrl:'/view/stulinfolist.jsp',  
      controller:'StuinfoController'  
  }).otherwise({  
      redirectTo:'/viewStudent'  
  });  
}]);






// Define the `PhoneListController` controller on the `phonecatApp` module
App.controller('PhoneListController', function PhoneListController($scope,$http) {
 
 
    $scope.phones = [
    {
      name: 'Nexus S',
      snippet: 'Fast just got faster with Nexus S.'
    }, {
      name: 'Motorola XOOM™ with Wi-Fi',
      snippet: 'The Next, Next Generation tablet.'
    }, {
      name: 'MOTOROLA XOOM™',
      snippet: 'The Next, Next Generation tablet.'
    }
  ];


  //对象定义--字符串对象
  $scope.remark='';

  $scope.user={
      username:'',
      pass:''
  }

//方法的定义与对象的获取

$scope.users=[];

$scope.addToArray=function(){

    console.log(`用户名是 `+$scope.user.username);
    console.log(`密码是 `+$scope.user.pass);
    $scope.users.push($scope.user);
    $scope.user={
        username:'',
        pass:''
    }

}


//从服务器端获取数据
$scope.total=0;
$scope.students=[];


// Simple GET request example:
$http({
    method: 'GET',
    url: '/getStuinlolist?page=1&rows=10'
  }).then(function successCallback(response) {
    console.dir(response);
    $scope.total=response.data.total;
    $scope.students=response.data.rows;



    }, function errorCallback(response) {
      // called asynchronously if an error occurs
     
    });







});




///学生信息管理

App.controller('StuinfoController', function StudentInfoListController($scope,$http,$location) {


//从服务器端获取数据
$scope.total=0;
$scope.students=[];


// Simple GET request example:
var parm={
  'page':1,
  'rows':10
}


$http.post(
     '/getStuinlolist',
     parm
  ).then(function successCallback(response) {
    console.dir(response);
    $scope.total=response.data.total;
    $scope.students=response.data.rows;



    }, function errorCallback(response) {
      // called asynchronously if an error occurs
     
    });



    $scope.student={
     stuinfoid:'',
      xh:'',
      xm:'',
       yx:'',
       bj:'',
       sfzh:'',
     xb:'',
       jg:'',
      mz:'',
      csny:'',
     zzmm:'',
       rdsj:'',
     lxdh:'',
     bj:''
    }




  


    $scope.delete=function(id){
      console.log(`方法参数`+id.stuinfoid);

      var config = {
        headers : {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
        }
    }
    
    ///参数
    var parm={
      'stuinfo.stuinfoid':id.stuinfoid,
     
    
    }
    
    //console.dir($scope.student);
    
    $http.post(
    '/deleteStuinfo',
    parm,
    config
    ).then(function successCallback(response) {
         $scope.refresh();  
    
    
    
    }, function errorCallback(response) {
    // called asynchronously if an error occurs
    alert(`删除异常，请刷新后再试`);
    });






    }
   
    $scope.addStuinfo=function(){
   var value='null';
     $location.path('/addStudent/'+value+'');

   
   
    }


    $scope.refresh=function(){
      $scope.total=0;
      $scope.students=[];
      
      
      // Simple GET request example:
      var parm={
        'page':1,
        'rows':10
      }
      
      
      $http.post(
           '/getStuinlolist',
           parm
        ).then(function successCallback(response) {
          console.dir(response);
          $scope.total=response.data.total;
          $scope.students=response.data.rows;
      
      
      
          }, function errorCallback(response) {
            // called asynchronously if an error occurs
           
          });


    }





});


//学生信息编辑
App.controller('EditStuInfoCtrl', function ($scope,$http,$location,$routeParams,$filter) {

console.log(`接收到的参数是`+$routeParams.id);

$scope.student={
  stuinfoid:'',
   xh:'',
   xm:'',
    yx:'',
    bj:'',
    sfzh:'',
  xb:'',
    jg:'',
   mz:'',
   csny:'',
  zzmm:'',
    rdsj:'',
  lxdh:''
 }

$scope.isAdd;

if($routeParams.id!='null'){

  console.log(`修改状态`);
  $scope.isAdd=false;
 //头文件，设置项
 var config = {
  headers : {
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
  }
}

///参数
var parm={
'stuinfo.stuinfoid':$routeParams.id,

}

//console.dir($scope.student);

$http.post(
'/getSingleStuinfo',
parm,
config
).then(function successCallback(response) {
    if(response.data.stuinfo!='error'){
       $scope.student=response.data.stuinfo;
       $scope.student.csny=response.data.stuinfo.csny==null?null:$filter('date')(response.data.stuinfo.csny.time, "yyyy-MM-dd");
       $scope.student.rdsj=response.data.stuinfo.rdsj==null?null:$filter('date')(response.data.stuinfo.rdsj.time, "yyyy-MM-dd");
    }
    else{
      alert(`数据获取异常，请刷新后再试`);
    }



}, function errorCallback(response) {
// called asynchronously if an error occurs
alert(`数据获取异常，请刷新后再试`);
});





}
else{
  console.log('添加状态');
  $scope.isAdd=true;
}




$scope.cancel=function(){

  $location.path('viewStudent');


}


$scope.save=function(){
     
if($scope.isAdd==true){
  $scope.add();
}
else{
  $scope.update();
}



 }




 //添加方法

$scope.add=function(){
  console.log(`添加方法调用`);

  //头文件，设置项
  var config = {
    headers : {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
    }
}

///参数
var parm={
  'stuinfo.xh':$scope.student.xh,
  'stuinfo.xm':$scope.student.xm,
  'stuinfo.yx':$scope.student.yx,
  'stuinfo.bj':$scope.student.bj,
  'stuinfo.sfzh':$scope.student.sfzh,
  'stuinfo.xb':$scope.student.xb,
  'stuinfo.jg':$scope.student.jg,
  'stuinfo.mz':$scope.student.mz,
  'stuinfo.csny':$scope.student.csny,
 'stuinfo.zzmm':$scope.student.zzmm,
  'stuinfo.rdsj':$scope.student.rdsj,
  'stuinfo.lxdh':$scope.student.lxdh,
 

}

//console.dir($scope.student);

$http.post(
'/addStuinfo',
parm,
config
).then(function successCallback(response) {
      if(response.data.msg==true){
        $location.path('viewStudent');
      }
      else{
        alert(`添加异常，请刷新后再试`);
      }



}, function errorCallback(response) {
// called asynchronously if an error occurs
alert(`添加异常，请刷新后再试`);
});

}



//修改方法

$scope.update=function(){
console.log(`修改方法调用`);

  //头文件，设置项
  var config = {
    headers : {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
    }
}

///参数
var parm={
  'stuinfo.stuinfoid':$scope.student.stuinfoid,
  'stuinfo.xh':$scope.student.xh,
  'stuinfo.xm':$scope.student.xm,
  'stuinfo.yx':$scope.student.yx,
  'stuinfo.bj':$scope.student.bj,
  'stuinfo.sfzh':$scope.student.sfzh,
  'stuinfo.xb':$scope.student.xb,
  'stuinfo.jg':$scope.student.jg,
  'stuinfo.mz':$scope.student.mz,
  'stuinfo.csny':$scope.student.csny,
 'stuinfo.zzmm':$scope.student.zzmm,
  'stuinfo.rdsj':$scope.student.rdsj,
  'stuinfo.lxdh':$scope.student.lxdh,
 

}

//console.dir($scope.student);

$http.post(
'/updateStuinfo',
parm,
config
).then(function successCallback(response) {
      if(response.data.msg==true){
        $location.path('viewStudent');
      }
      else{
        alert(`添加异常，请刷新后再试`);
      }



}, function errorCallback(response) {
// called asynchronously if an error occurs
alert(`添加异常，请刷新后再试`);
});


}






//初始化搬家下拉列表

$scope.yxlist=[];

$http.get(
  '/getDepartmentList'

  ).then(function successCallback(response) {
    
  $scope.yxlist=response.data.rows;
  
  
  }, function errorCallback(response) {
  // called asynchronously if an error occurs
  alert(`添加异常，请刷新后再试`);
  });





});

